package com.gaofei.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @RequestMapping("list")
    public String list() throws Exception {
        /*FileReader fr = new FileReader(new File("D:\\2105A\\springcloud-2105a\\user-server\\user-server-service\\src\\main\\resources\\aa.txt"));
        int len = 0;
        char[] ch = new char[1024];
        while ((len=fr.read(ch))!=-1){
//            System.out.println(new java.lang.String(ch,0,len));
        kafkaTemplate.send("2105A", "aa",new java.lang.String(ch,0,len));
        }*/


        ListenableFuture<SendResult<String, String>> send = kafkaTemplate.send("2105A", "aaaa", "你好啊哈哈哈哈");

        send.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onFailure(Throwable throwable) {
                //生产者发送失败了,就会走这个方法
                //1.把发送失败 的消息临时存储起来(redis,mysql...)
                //2.开个定时任务,每天晚上的12:00钟-->从数据库查询出来这些失败的消息,重新发送
                System.out.println("发送失败会走这个发法");
            }

            @Override
            public void onSuccess(SendResult<String, String> stringStringSendResult) {
                //生产者发送成功了,就会走这个方法
//                发送成功会走这个方法
            }
        });
//        SendResult<String, String> stringStringSendResult = send.get();
//        System.out.println("======================================");
//        System.out.println(stringStringSendResult.getProducerRecord());
        return "user  list  !!";
    }
}
